home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1793 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  87 lines

  1. Newsgroups: comp.lang.c
  2. Path: uu4news.netcom.com!zodiac!szh
  3. From: szh@zcon.com (Syed Zaeem Hosain)
  4. Subject: Re: A Very Simple Socket Question
  5. Message-ID: <1996Jan16.212537.2710@zcon.com>
  6. Sender: szh@zcon.com (Syed Zaeem Hosain)
  7. Nntp-Posting-Host: zodiac
  8. Reply-To: szh@zcon.com
  9. Organization: Z Consulting Group
  10. References: <1996Jan16.174631.1892@zcon.com>
  11. Date: Tue, 16 Jan 1996 21:25:37 GMT
  12.  
  13. In article <1996Jan16.174631.1892@zcon.com>, szh@zcon.com (Syed Zaeem Hosain) writes:
  14. >In article <4dfgj8$6p9@nntp.ucs.ubc.ca>, evil@unixg.ubc.ca (Peter Pan) writes:
  15. >>Please help! I am a C beginner.
  16. >>
  17. >>======================================
  18. >>Client:
  19. >>
  20. >>int sd, addrlen;
  21. >>struct sockaddr_in svaddr;
  22. >>
  23. >>char *data;
  24. >>data = "abcdef";
  25. >>
  26. >>..... /* skip */
  27. >>
  28. >>sendto(sd, data, strlen(data), 0, &svaddr, &addrlen);
  29. >>
  30. >>
  31. >>==========================================
  32. >>Server:
  33. >>
  34. >>char data;
  35. >>data = (char*) malloc( 100*sizeof(char) );
  36. >>
  37. >>recvfrom(sd2, data, sizeof(data), 0, &claddr, &addrlen);
  38. >>
  39. >>==========================================
  40. >>
  41. >>Output:
  42. >>
  43. >>data sent: abcdef
  44. >>data received: abcd
  45. >>
  46. >>==========================================
  47. >>
  48. >>Anyone knows why the data are different after transmittion.
  49. >
  50. >Well, I do not know if this is the problem (it may be a typo in your
  51. >post), but you might try declaring:
  52. >
  53. >char *data;
  54. >
  55. >on the server, rather than:
  56. >
  57. >char data;
  58. >
  59. >When you then assign "data = (char *) mall...", the very explicit cast
  60. >*probably* (but I am not sure) causes the error messages about this
  61. >mistake to be suppressed.
  62. >
  63. >Of course, there may still be something more wrong with the socket
  64. >transmission. But this is one place to get started.
  65.  
  66. Ah! Upon a re-read on the server, I also see that the buffer size has
  67. been sent as "sizeof(data)". This is merely the size of a char pointer
  68. on your system (assuming that it is "char *data;" that you actually
  69. have in the code).
  70.  
  71. So you need to change this to 100 * size of char, although sizeof(char)
  72. is 1, so you could simply use:
  73.  
  74. recvfrom(sd2, data, 100, 0, &claddr, &addrlen);
  75.  
  76. since you explicitly use 100 in the call to malloc rather than a macro
  77. definition or variable.
  78.  
  79.                                 Z
  80.  
  81.  
  82. -- 
  83. -------------------------------------------------------------------------
  84. | Syed Zaeem Hosain          P. O. Box 610097            (408) 441-7021 |
  85. | Z Consulting Group        San Jose, CA 95161             szh@zcon.com |
  86. -------------------------------------------------------------------------
  87.